﻿Imports EstoqueBLL
Imports EstoqueDAL
Public Class ServicoCAD
    Dim objBLL As New ServicoBLL
    Dim operacao As Char
    Dim objcategoria As New CategoriaBLL
#Region "Eventos"
#Region "Evento key press"
    Private Sub eventoKeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TVenda.KeyPress, TCusto.KeyPress
        If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
        Dim obj As New TextBox
        obj = sender
        If (sender.Text.Contains(",") = False And e.KeyChar = ",") Then e.Handled = False
    End Sub
#End Region

#Region "Form Closing"
    Private Sub ServicoCAD_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        limpar()
    End Sub
#End Region

#Region "Load"
    Private Sub ServicoCAD_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'sta_codigo: 1- ativo, 2 inativo
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo=1 or sta_codigo=2", "sta_descricao")
        If (Not IsNothing(LCodigo.Text) And LCodigo.Text.Trim <> "") Then
            objBLL = New ServicoBLL(Val(LCodigo.Text))
            carregacampos()
            operacao = "A"
            LCodigo.Visible = True
        Else : operacao = "I"
        End If
    End Sub
#End Region

#Region "KeyDown Form"
    Private Sub ServicoCAD_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
#End Region

#Region "Salvar"
    Private Sub TSBGravar_Click(sender As System.Object, e As System.EventArgs) Handles TSBGravar.Click
        Try
            'carregar campos
            objBLL = New ServicoBLL(Val(LCodigo.Text), StrConv(LCase(RTrim(LTrim(TDescricao.Text))), VbStrConv.ProperCase), CBStatus.SelectedValue, Decimal.Parse(TVenda.Text.Replace("R$", "")), Decimal.Parse(TCusto.Text.Replace("R$", "")), objcategoria.cat_codigo, FormatDateTime(dtduracao.Text, DateFormat.LongTime))
            If operacao = "I" Then

                If (objBLL.inserir(objusuarios.Codigo)) Then
                    MessageBox.Show("Serviço inserido com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.Close()
                Else : MessageBox.Show("O serviço não pôde ser inserido", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            Else
                If (objBLL.alterar(objusuarios.Codigo)) Then
                    MessageBox.Show("Serviço alterado com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.Close()
                Else : MessageBox.Show("O serviço não pôde ser alterado", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region

#Region "Textbox Custo"
    Private Sub TCusto_LostFocus(sender As Object, e As System.EventArgs) Handles TCusto.LostFocus
        TCusto.Text = FormatCurrency(TCusto.Text)
    End Sub
#End Region

#Region "Textbox Venda"
    Private Sub TVenda_LostFocus(sender As Object, e As System.EventArgs) Handles TVenda.LostFocus
        TVenda.Text = FormatCurrency(TVenda.Text)
    End Sub
#End Region

#Region "Busca Categoria"
    Private Sub BBuscaCategoria_Click(sender As System.Object, e As System.EventArgs) Handles BBuscaCategoria.Click
        'buscar categoria
        'buscar categoria e gravar
        Dim objcat As New CategoriaCON
        objcat.tipo = "ret"
        objcat.ShowDialog()
        If (Not IsNothing(objcat.objcate)) Then
            objcategoria = objcat.objcate
            tcategoria.Text = objcategoria.cat_descricao
        End If
    End Sub
#End Region
#End Region

#Region "Métodos"
#Region "Limpar"
    Public Sub limpar()
        Me.TDescricao.Clear()
        Me.CBStatus.SelectedValue = 0
        Me.objcategoria = Nothing
        Me.TCusto.Clear()
        Me.TVenda.Clear()
        Me.objBLL = Nothing
        Me.lcodigo.Text = Nothing
    End Sub
#End Region

#Region "Carregar Campos"
    Public Sub carregacampos()
        TDescricao.Text = objBLL.pro_descricao
        CBStatus.SelectedValue = objBLL.sta_codigo
        Me.objcategoria = New CategoriaBLL(objBLL.cat_codigo)
        Me.tcategoria.Text = objcategoria.cat_descricao
        TVenda.Text = objBLL.pro_precovenda
        TCusto.Text = objBLL.pro_precocusto
        LCodigo.Text = objBLL.pro_codigo
    End Sub
#End Region
#End Region

End Class